
import { observable, computed, action } from 'mobx'
import { createContext } from 'react';
import ChildrenPageUtils from "@/store/extends";
import Taro from '@tarojs/taro';
type FileType = 'doc' | 'docx' | 'xls' | 'xlsx' | 'ppt' | 'pptx' | 'pdf'


class Productfileinfo extends ChildrenPageUtils {
  @observable pageQuery: { devId: string | number } & Setting.FileData = {
    devTypePFId: 0,
    devTypeId: '',
    devTypePFTypeCode: '',
    devTypePFTypeName: '',
    devTypePFName: '',
    devTypePFUrl: '',
    devTypePFPath: '',
    devTypePFDesc: null,
    createTime: '',
    devId: 0
  }

  @action.bound
  checkFile() {
    // toWebView({
    //   devId: pageQuery.devId,
    //   title: pageQuery.devTypePFName,
    //   // url: pageQuery.devTypePFUrl + '/' + pageQuery.devTypePFPath
    //   url: 'https://d.lohand.com:10001/instructionBook/32孔智能消解仪(中文)20220629133504.pdf'
    // })

    const fileType: FileType = this.pageQuery.devTypePFPath.split(
      '.'
    )[1] as FileType

    if (!fileType)
      return Taro.showModal({
        content: '无法获取文件类型'
      })

    Taro.showLoading({
      title: '打开中...'
    })

    Taro.downloadFile({
      url: this.pageQuery.devTypePFUrl + '/' + this.pageQuery.devTypePFPath, // 这里换上自己的pdf地址
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var Path = res.tempFilePath // 返回的文件临时地址，用于后面打开本地预览所用
        Taro.openDocument({
          filePath: Path,
          fileType: fileType, // 指定文件类型
          success: function (res) {
            Taro.hideLoading()
          },
          fail: function (res) {
            console.log('打开失败', res)
            Taro.hideLoading()
          }
        })
      },
      fail: function (res) {
        Taro.hideLoading()
        Taro.showToast({
          title: '下载失败',
          icon: 'error',
          duration: 1000
        })
      }
    })
  }

  @action.bound
  clearPageData() {

  }
}


export const productFileInfoStore = createContext(new Productfileinfo())
